package main

import "fmt"

type Teacher struct {
	name string
	age  int8
	sex  byte
}

func main() {

	t1 := Teacher{}
	t1.name = "json"
	t1.age = 8
	t1.sex = 1

	t2 := Teacher{name: "json", age: 8, sex: 1}

	t3 := Teacher{"json", 8, 1}
	fmt.Println("t1.name == t2.name == t3.name ? ", t1.name == t2.name, t1.name == t3.name)

	p1 := new(Teacher)
	(*p1).name = "json"
	(*p1).age = 8
	(*p1).sex = 1
	fmt.Println(" (*p1).name ==  ", (*p1).name)

	//第二种 语法糖赋值
	p1.name = "sugar"
	fmt.Println(" (*p1).name ==  ", (*p1).name, " p1.name == ", p1.name)

	//数组中的语法糖
	arr1 := [3]int{1, 2, 3}
	arr2 := &arr1
	fmt.Println("arr2 第1个元素是 ", arr2[0])
	arr2[0] = 4
	fmt.Println("arr2 第1个元素是 ", arr2[0], " arr1 第1个元素是 ", arr1[0])

	//切片中的语法糖
	sli1 := []int{1, 2, 3}
	sli2 := &sli1
	fmt.Println("sli2 第1个元素是 ", (*sli2)[0])
	(*sli2)[0] = 4
	fmt.Println("sli2 第1个元素是 ", (*sli2)[0], " sli2 第1个元素是 ", (*sli2)[0])

	// 证明函数是值类型,属于深拷贝,浅拷贝只是复制了指针
	t4 := t3
	t4.name = "sugar"
	fmt.Println("t3.name == ", t3.name, " t4.name == ", t4.name)

	t5 := getTeacher()
	t6 := getTeacher()
	fmt.Printf("t5 内存地址 ==  %p  , t6 内存地址 == %p  \n", &t5, &t6)
	fmt.Println("t5  == ", t5, " t6  == ", t6)
	t5.name = "java"
	//深拷贝 只有t5的name值修改了,t6的没有受到影响
	fmt.Println("t5  == ", t5, " t6  == ", t6)

	//函数返回指针
	t7 := getTeacher2()
	t8 := getTeacher2()
	fmt.Printf("t7 内存地址 ==  %p  , t8 内存地址 == %p  \n", &t7, &t8)
	fmt.Println("t7  == ", t7, " t8  == ", t8)
	t7.name = "java"
	//深拷贝 只有t7的name值修改了,t8的没有受到影响
	fmt.Println("t7  == ", t7, " t8  == ", t8)
}

func getTeacher() Teacher {
	t1 := Teacher{"go", 3, 1}
	return t1
}

func getTeacher2() *Teacher {
	t1 := &Teacher{"go", 3, 1}
	return t1
}
